<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Gfp extends CI_Controller
{
      function __construct()
      {
            parent::__construct();
            $this->load->helper(array('form', 'url'));
            $this->load->library('form_validation');
            $this->load->model('user');
      }

      public function index()
      {
      	
      $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|callback_email_check');
 
       if ($this->form_validation->run() == FALSE)
      {
             $this->load->view('email_check_view');
      }
       else
      {
			$email= $this->input->post('email');
			
			$this->load->helper('string');
			$rs= random_string('alnum', 12);
			$data = array(
			               'rs' => $rs
			            );
			$this->user->update_rs($data);
			
			//now we will send an email
			
			 $config['protocol'] = 'smtp';
			 $config['smtp_host'] = 'ssl://smtp.googlemail.com';
			 $config['smtp_port'] = 465;
			 $config['smtp_user'] = 'somemail@gmail.com';
			 $config['smtp_pass'] = '************';
			       
			          
			$this->load->library('email', $config);
			
			$this->email->from('somemail@gmail.com', 'Ikz.php');
			$this->email->to($email);
			
			$this->email->subject('Get your forgotten Password');
			$this->email->message('Please go to this link to get your password.
			       http://localhost/MyWebSite/index.php/get_password/index/'.$rs );
			
			$this->email->send();
			echo "Please check your email address.";
      }
 	 }
	public function email_check($str)
	{
	$query = $this->db->get_where('user', array('email' => $str), 1);
	 
	      if ($query->num_rows()== 1)
	      {
	          return true;
	      }
	      else
	      {    
	          $this->form_validation->set_message('email_check', 'This Email does not exist.');
	          return false;
	
	      }
	   }    
}
?>